package cn.bonoon.kernel.web.handlers;

import java.lang.reflect.AnnotatedElement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.bonoon.kernel.util.MD5Util;
import cn.bonoon.kernel.web.annotations.DialogId;

/**
 * <pre>
 * 对应一个访问路径的处理器，如：打开界面、提交数据等url处理
 * </pre>
 * @author jackson
 *
 */
public abstract class AbstractExecuteHandler implements ExecutableHandler{

	protected final Log logger = LogFactory.getLog(getClass());
	
	/** 这里生成的是相对路径 */
	protected String url;
	
	/** 每一个功能的唯一值 */
	protected final String key;

	private static String parseKey(AnnotatedElement ae){
		if(null != ae){
			DialogId di = ae.getAnnotation(DialogId.class);
			if(null != di){
				return di.value();
			}
		}
		return MD5Util.randomMD5String();
	}

	protected AbstractExecuteHandler(AnnotatedElement ae, String ext){
		this(parseKey(ae), ext);
		//没有被注册的情况
//		this.key = parseKey(ae);
//		this.url = key + "!exe." + ext;
	}
	protected AbstractExecuteHandler(String ext){
		this(MD5Util.randomMD5String(), ext);
		//没有被注册的情况
//		this.key = MD5Util.randomMD5String();
//		this.url = key + "!exe." + ext;
	}
	protected AbstractExecuteHandler(String key, String ext){
		this.key = key;
		this.url = key + "!exe." + ext;
	}
	
	
	@Override
	public String getKey() {
		return key;
	}
	
	@Override
	public String getUrl() {
		return url;
	}
	
	@Override
	public Object buildParams() {
		return null;
	}
}
